<?php
namespace Modules\ShopApi\Http\Controllers\v1;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use zgldh\QiniuStorage\QiniuStorage;
use Modules\Admin\Models\Image as ImageModel;
use Modules\ShopApi\Models\BaseApiModel;
use Illuminate\Support\Facades\DB;
class UploadController extends CommonController
{
	/**
	 * @name  单图上传
	 * @method  POST	
	 * @return  JSON	
	 */
	public function upload_file(Request $request){
    	if ($request->isMethod('POST')){
    		$fileCharater = $request->file('file');
    		if($fileCharater->isValid()){
				$image_status = DB::table('configs')->where('id',1)->value('image_status');
				if($image_status == 1){
					$path = $request->file('file')->store(date('Ymd'),'shop_upload');
					if($path){
						$url = '/upload/shop_image/'.$path;
					}	
				}else if($image_status == 2){
					$url = $this->add_qiniu($fileCharater);
				} 
				if(isset($url)){
					$image_id = ImageModel::insertGetId([
						'url'=>$url,
						'open'=>$image_status,
						'status'=>0,
						'created_at'=> date('Y-m-d H:i:s')
					]);
					if($image_id){
						if($image_status == 1){
							$url = getHttpType() . $url;
						}
						return (new BaseApiModel())->api_success(
							[
								'data'=>[
									'image_url'=> $url,
									'image_id'=>$image_id
								]
							]
						);
					}
				}		
			}
			return (new BaseApiModel())->api_error();
    	}
    }
	/**
	 * @name 七牛云图片上传
	 * @param fileCharater 图片对象
	 * @return 返回上传路径
	 */
	private function add_qiniu($fileCharater){
		// 初始化
		$disk = QiniuStorage::disk('qiniu');
		// 重命名文件
		$fileName = md5($fileCharater->getClientOriginalName().time().rand()).'.'.$fileCharater->getClientOriginalExtension();
		 // 上传到七牛
		$bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($fileCharater->getRealPath()));
		// 判断是否上传成功
		if($bool){
			return $disk->downloadUrl('iwanli/image_'.$fileName);  
		}else{
			return false;
		}
	}
	/**
	 * @name  生成二维码
	 * @method  GET		
	 * @param   text 用来生成二维码的内容（一般使用网址）
	 * @param   size 生成二维码大小
	 * @param   logo_status 是否代logo  1=是 0=否
	 * @param   status 是否二维码图片  1=是 0=否
	 * @param color 颜色
	 * @param backgroundColor 背景颜色
     * @param margin 外边距
	 */ 
	public function get_api_code(Request $request){
		$data = $request->all();
		$BaseApiModel = new BaseApiModel();
		if(!isset($data['text'])){
			return $BaseApiModel->api_error(['msg'=>'请填写生成二维码的内容！']);
		}
		$code_data['text'] = $data['text'];
		if(isset($data['size']) && ($data['size'] + 0) > 0){
			$code_data['size'] = $data['size'] + 0;
		}
		if(isset($data['logo_status']) && $data['logo_status'] == 1){
			$code_data['logo_url'] = 'logo.png';
		}
		if(isset($data['margin']) && ($data['margin'] + 0) > 0){
			$code_data['margin'] = $data['margin'] + 0;
		}
		if(isset($data['color'])){
			$code_data['color'] = explode(',',$data['color']);
		}
		if(isset($data['backgroundColor'])){
			$code_data['backgroundColor'] = explode(',',$data['backgroundColor']);
		}
		if(isset($data['status']) && $data['status'] == 1){
			$code_data['code_url'] = 'upload/code/'. time() . mt_rand(10000,99999) .'.png';
			get_code($code_data);
			return $BaseApiModel->api_success(
						[
							'data'=>[
								'code_url'=>getHttpType() . '/' .$code_data['code_url']
							]
						]
			);
		}else{
			return get_code($code_data);
		}
		
	}
}
